﻿{extend name="common/base" /}
{block name="title"}新建网站角色 - 管理员管理{/block}
{block name="body"}
	<article class="clearfix pd-20">
		<form action="" method="post" class="form form-horizontal" id="form-admin-role-add">
			<div class="row clearfix">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
				<div class="form-controls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="" placeholder="" id="roleName" name="roleName" datatype="*4-16" nullmsg="用户账户不能为空">
				</div>
			</div>
			<div class="row clearfix">
				<label class="form-label col-xs-4 col-sm-3">备注：</label>
				<div class="form-controls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="" placeholder="" id="desc" name="desc">
				</div>
			</div>
			<div class="row clearfix">
				<label class="form-label col-xs-4 col-sm-3">网站角色：</label>
				<div class="form-controls col-xs-8 col-sm-9">
                    {foreach $list as $k=>$v}
                        
					<dl class="Hui-admin-permission-list">
						<dt>
							<label>
								<input type="checkbox" value="{$v.id}" name="user-Character-{$k}" id="user-Character-{$k}">
								{$v.auth_name}</label>
						</dt>
						<dd>
                            {if isset($v.child) && is_array($v.child)}
                                {foreach $v.child as $k1=>$v1}
							<dl class="clearfix Hui-admin-permission-list2">
								<dt>
									<label class="">
										<input type="checkbox" value="{$v1.id}" name="user-Character-{$k}-{$k1}" id="user-Character-{$k}-{$k1}">
										{$v1.auth_name}</label>
								</dt>
								<dd>
                                    {if isset($v1.child) && is_array($v1.child)}
                                        {foreach $v1.child as $k2=>$v2}
									<label class="">
										<input type="checkbox" value="{$v2.id}" name="user-Character-{$k}-{$k1}-{$k2}" id="user-Character-{$k}-{$k1}-{$k2}">
										{$v2.auth_name}</label>
                                        {/foreach}
									{/if}
								</dd>
							</dl>
                                {/foreach}
                            {/if}
							
						</dd>
					</dl>
                       
                    {/foreach}
					
				</div>
			</div>
			<div class="row clearfix">
				<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
					<button type="submit" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
				</div>
			</div>
		</form>
	</article>
    <input type="hidden" name="add" value="{:url('role/add')}">

{/block}
{block name="js"}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
$(function(){
    $(".Hui-admin-permission-list dt input:checkbox").click(function(){
        $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
    });
    $(".Hui-admin-permission-list2 dd input:checkbox").click(function(){
        var l =$(this).parent().parent().find("input:checked").length;
        var l2=$(this).parents(".Hui-admin-permission-list").find(".Hui-admin-permission-list2 dd").find("input:checked").length;
        if($(this).prop("checked")){
            $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
            $(this).parents(".Hui-admin-permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
        }
        else{
            if(l==0){
                $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
            }
            if(l2==0){
                $(this).parents(".Hui-admin-permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
            }
        }
    });

    $("#form-admin-role-add").validate({
        rules:{
            roleName:{
                required:true,
            },
            desc:{
                required:true,
            },
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            //$(form).ajaxSubmit();
            //var index = parent.layer.getFrameIndex(window.name);
            //parent.layer.close(index);
            var auth_id = [];
            $("input[type='checkbox']:checked").each(function(index, ele){
                auth_id.push($(ele).val());
            });
            var auth_ids = auth_id.join(',');
            var url = $("input[name='add']").val();
            var roleName = $("input[name='roleName']").val();
            var desc = $("input[name='desc']").val();
            $.ajax({
                url : url,
                data : { 
                    'name' : roleName,
                    'desc' : desc,
                    'auth_ids' : auth_ids
                },
                type : 'post',
                dataType : 'json',
                success : function(ret){
                    if(0 == ret.code){
                        layer.msg(ret.msg, { icon:1,time:1000 });
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.replace(parent.location.href)
                        parent.layer.close(index);
                    }else{
                        layer.msg(ret.msg, { icon:2,time:1000 });
                    }
                }
            });
        }
    });
});
</script>
<!--/请在上方写此页面业务相关的脚本-->
{/block}